package com.hhd.flow.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhd.flow.engine.FlowEngine;
import com.hhd.flow.engine.utils.CommonUtils;
import com.hhd.flow.engine.utils.FlowUtils;
import com.hhd.flow.entity.*;
import com.hhd.flow.mapper.FlowActionLogMapper;
import com.hhd.flow.service.IFlowActionLogService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tangzy
 */
@Service
@Log
public class FlowActionServiceImpl extends ServiceImpl<FlowActionLogMapper, FlowActionLog> implements IFlowActionLogService {

    @Lazy
    @Autowired
    FlowEngine flowEngine;

    @Override
    public void logForStartProcess(FlowProcessInstance flowProcessInstance) {
        try {
            FlowActionLog log = new FlowActionLog();
            FlowUtils.setLogInfo(flowProcessInstance, log, flowEngine);
            FlowUtils.setLogUserInfo(log, flowEngine);
            log.setActionType("发起流程");
            save(log);
        } catch (Exception e) {
            log.info(e.toString());
            e.printStackTrace();
        }
    }

    @Override
    public void logForCompleteProcess(String flowUserTaskId, String comment) {
        try {
            FlowActionLog log = new FlowActionLog();
            FlowUserTask flowUserTask = flowEngine.getFlowUserTaskService().getById(flowUserTaskId);
            FlowUtils.setLogInfo(flowUserTask, log, flowEngine);
            FlowUtils.setLogUserInfo(log, flowEngine);
            log.setActionType("同意");
            log.setMetaData(comment);
            save(log);
        } catch (Exception e) {
            log.info(e.toString());
            e.printStackTrace();
        }
    }

    @Override
    public void logForEndProcess(String flowUserTaskId, String comment) {
        try {
            FlowActionLog log = new FlowActionLog();
            FlowUserTask flowUserTask = flowEngine.getFlowUserTaskService().getById(flowUserTaskId);
            FlowUtils.setLogInfo(flowUserTask, log, flowEngine);
            FlowUtils.setLogUserInfo(log, flowEngine);
            log.setActionType("不通过");
            log.setMetaData(comment);
            save(log);
        } catch (Exception e) {
            log.info(e.toString());
            e.printStackTrace();
        }
    }

    @Override
    public void logForToFormProcess(String flowUserTaskId, String comment) {
        try {
            FlowActionLog log = new FlowActionLog();
            FlowUserTask flowUserTask = flowEngine.getFlowUserTaskService().getById(flowUserTaskId);
            FlowUtils.setLogInfo(flowUserTask, log, flowEngine);
            FlowUtils.setLogUserInfo(log, flowEngine);
            log.setActionType("回退给发起人");
            log.setMetaData(comment);
            save(log);
        } catch (Exception e) {
            log.info(e.toString());
            e.printStackTrace();
        }
    }

    @Override
    public void logForWithdrawProcess(String processInstanceId, String comment) {
        try {
            FlowActionLog log = new FlowActionLog();
            FlowProcessInstance flowProcessInstance = flowEngine.getFlowProcessInstanceService().getById(processInstanceId);
            FlowUtils.setLogInfo(flowProcessInstance, log, flowEngine);
            FlowUtils.setLogUserInfo(log, flowEngine);
            log.setMetaData(comment);
            log.setActionType("发起人主动撤回");
            log.setMetaData(comment);
            save(log);
        } catch (Exception e) {
            log.info(e.toString());
            e.printStackTrace();
        }
    }

    @Override
    public void logForPassProcess(String flowUserTaskId) {
        try {
            FlowUserTaskHistory userTaskHistory = flowEngine.getFlowUserTaskHistoryService().getById(flowUserTaskId);
            if (CommonUtils.isEmpty(userTaskHistory)) return ;
            FlowProcessInstanceHistory flowProcessInstanceHistory = flowEngine.getFlowProcessInstanceHistoryService().getById(userTaskHistory.getProcessInstanceId());
            if (CommonUtils.isEmpty(flowProcessInstanceHistory)) return ;

            FlowActionLog log = new FlowActionLog();
            FlowUtils.setLogInfo(userTaskHistory, flowProcessInstanceHistory, log, flowEngine);
            log.setActionType("流程结束了");
            FlowUtils.setLogUserInfo(log, flowEngine);
            save(log);
        } catch (Exception e) {
            log.info(e.toString());
            e.printStackTrace();
        }
    }
}
